define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'jy/product_download/index' + location.search,
                    add_url: 'jy/product_download/add',
                    edit_url: 'jy/product_download/edit',
                    del_url: 'jy/product_download/del',
                    multi_url: 'jy/product_download/multi',
                    import_url: 'jy/product_download/import',
                    table: 'product_download',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'jpd.id',
                search: false,
                undefinedText: '',
                exportOptions: {
                    fileName: 'export_' + Moment().format("YYYY-MM-DD"),
                    ignoreColumn: ['operate'] //默认不导出第一列(checkbox)与操作(operate)列
                },
                queryParams: function (params) {
                    var filter = JSON.parse(params.filter);
                    var op = JSON.parse(params.op);
                    var sort = params.sort;
                    if (sort === 'update_time') {
                        params.sort = 'blog.update_time';
                    }
                    if (filter.id) {
                        filter['jpd.id'] = filter.id;
                        op['jpd.id'] = op.id;
                        delete filter['id'];
                        delete op['id'];
                    }
                    if (filter.user_id) {
                        filter['jpd.user_id'] = filter.user_id;
                        op['jpd.user_id'] = op.user_id;
                        delete filter['user_id'];
                        delete op['user_id'];
                    }
                    if (filter.product_id) {
                        filter['jpd.product_id'] = filter.product_id;
                        op['jpd.product_id'] = op.product_id;
                        delete filter['product_id'];
                        delete op['product_id'];
                    }
                    if (filter.product_name) {
                        filter['jp.product_name'] = filter.product_name;
                        op['jp.product_name'] = op.product_name;
                        delete filter['product_name'];
                        delete op['product_name'];
                    }
                    if (filter.product_id) {
                        filter['jp.product_id'] = filter.product_id;
                        op['jp.product_id'] = op.product_id;
                        delete filter['product_id'];
                        delete op['product_id'];
                    }
                    if (filter.nickname) {
                        filter['ju.nickname'] = filter.nickname;
                        op['ju.nickname'] = op.nickname;
                        delete filter['nickname'];
                        delete op['nickname'];
                    }
                    if (filter.mobile) {
                        filter['ju.mobile'] = filter.mobile;
                        op['ju.mobile'] = op.mobile;
                        delete filter['mobile'];
                        delete op['mobile'];
                    }
                    if (filter.nickname2) {
                        filter['ju2.nickname'] = filter.nickname2;
                        op['ju2.nickname'] = op.nickname2;
                        delete filter['nickname2'];
                        delete op['nickname2'];
                    }
                    if (filter.mobile2) {
                        filter['ju2.mobile'] = filter.mobile2;
                        op['ju2.mobile'] = op.mobile2;
                        delete filter['mobile2'];
                        delete op['mobile2'];
                    }
                    if (filter.create_time) {
                        filter['jpd.create_time'] = filter.create_time;
                        op['jpd.create_time'] = "RANGE";
                        delete filter['create_time'];
                        delete op['create_time'];
                    }
                    params.filter = JSON.stringify(filter);
                    params.op = JSON.stringify(op);
                    return params;
                },
                columns: [
                    [
                        {field: 'id', title: __('Id')},
                        {field: 'product_id', title: __('Product_id')},
                        {field: 'product_name', title: __('Product.product_name'), operate: 'LIKE'},
                        {
                            field: 'cover_images',
                            title: __('Product.cover_images'),
                            operate: false,
                            events: Table.api.events.image,
                            formatter: Table.api.formatter.images
                        },
                        {field: 'user_id', title: __('User_id')},
                        {field: 'nickname', title: __('User.nickname'), operate: 'LIKE'},
                        {field: 'mobile', title: __('User.mobile'), operate: 'LIKE'},
                        {field: 'nickname2', title: __('邀请用户昵称'), operate: 'LIKE'},
                        {field: 'mobile2', title: __('邀请用户电话'), operate: 'LIKE'},
                        {
                            field: 'create_time',
                            title: __('Create_time'),
                            operate: 'RANGE',
                            // operate: false,
                            addclass: 'datetimerange',
                            autocomplete: false
                        },
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});